package com.erp.myAdapter

import android.content.Context
import android.content.Intent
import android.os.Vibrator
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.ImageView
import android.widget.TextView
import com.erp.activity.DanjianivActivity
import com.erp.bean.DanjuBean
import com.erp.Interface.DisplayElemrnt
import com.erp.Interface.OnUserDataInterface
import com.erp.R
import com.erp.util.Httputil
import com.erp.util.JsonUtil
import com.erp.util.Myutil
import com.erp.util.Sputil
import com.erp.view.MyAlerDialog
import com.erp.view.ScrollListview
import kotlinx.android.synthetic.main.lv_danju.view.*


/**
 * Created by Administrator on 2017/2/10/010.
 * 作者：刘洋
 */
class DanjuAdapter(private val mcontext: Context, private var mlist: List<DanjuBean>, private val type: Int, private val display: DisplayElemrnt) : BaseAdapter() {
    private val minflater: LayoutInflater = LayoutInflater.from(mcontext)

    override fun getCount(): Int {
        return mlist.size
    }

    override fun getItem(position: Int): DanjuBean {
        return mlist[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
        var v : View
        val vh: ViewHolder
        if (convertView == null) {
            v = minflater.inflate(R.layout.lv_danju, null)
            vh = ViewHolder(v)
            v.tag = vh
        } else {
            v = convertView
            vh = convertView.tag as ViewHolder
        }
        val danjuBean = mlist!![position]
        Myutil.showhttpphoto(danjuBean.uri, vh.ivDanju, mcontext)
        Myutil.settv(Myutil.getdata(danjuBean.ctime), vh.tvCtime)
        Myutil.settv(danjuBean.creator_name, vh.tvCreatorName)
        val adapter = DanjuDingdanAdapter(mcontext, danjuBean.onums)
        vh.lvDanjuDingdan!!.adapter = adapter
        vh.ivDanju!!.setOnClickListener {
            val intent = Intent(mcontext, DanjianivActivity::class.java)
            intent.putExtra("url", danjuBean.uri)
            mcontext.startActivity(intent)
        }
        vh.ivDanju!!.setOnLongClickListener {
            if (Sputil.sp("erp").getBoolean("isdeletedanju", false)) {
                val vibrator = mcontext.getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
                mcontext.getSystemService(Context.VIBRATOR_SERVICE)
                vibrator.vibrate(100)
                val myAlerDialog = MyAlerDialog(mcontext)
                myAlerDialog.start()
                myAlerDialog.setdata("确定要删除吗？")
                myAlerDialog.setleft("确定") {
                    val updialog = Myutil.showupdatedialog(mcontext, "正在删除图片", false)
                    Httputil.deletedanju(type, mcontext, danjuBean.fid, object : OnUserDataInterface {
                        override fun onUserData(data: String) {
                            JsonUtil.toost(data)
                            updialog?.dismiss()
                            display.dispaly()
                        }

                        override fun onNot(throwable: String) {
                            updialog?.dismiss()
                        }
                    })
                    myAlerDialog.getdialog().dismiss()
                }
                myAlerDialog.setright("取消") { myAlerDialog.getdialog().dismiss() }
                myAlerDialog.show()
            } else {
                val myAlerDialog = MyAlerDialog(mcontext)
                myAlerDialog.start()
                myAlerDialog.setdata("您不具备删除单据的权限，请找管理员索要！")
                myAlerDialog.setleft("确定") { myAlerDialog.getdialog().dismiss() }
                myAlerDialog.show()
            }
            false
        }
        return v
    }

    fun Allnotify(list: List<DanjuBean>?) {
        if (list != null) {
            mlist = list
            notifyDataSetChanged()
        }
    }

    class ViewHolder(v: View) {
        var lvDanjuDingdan: ScrollListview = v.lv_danju_dingdan
        var ivDanju: ImageView = v.iv_danju
        var tvCtime: TextView = v.tv_ctime
        var tvCreatorName: TextView = v.tv_creator_name
    }
}
